#include <stdio.h>
#include <stdlib.h>
#include <assert.h>


#include "list.h"

struct list* list_create(){
  struct list * list = malloc (sizeof (struct list));
  list->t1 = NULL;
  list->next = list;
  list->previous = list;
  return list;
}

//ajout en tête de liste
void list_add(struct list ** l, void* *t1){
  if ((*l)->t1 == NULL){
    (*l)->t1 = t1;
  }
  else{
    struct list * l2 = malloc (sizeof(struct list));
    l2->t1 = t1;
    l2->next = (*l)->next;
    l2->previous = *l;
    (*l)->next->previous = l2;
    (*l)->next = l2;
    *l=(*l)->next;
  }
}

void list_remove(struct list** l){
  if(*l==(*l)->next){
   free(*l);
   *l=NULL;
  }
  else{ 
  (*l)->next->previous = (*l)->previous;
  (*l)->previous->next = (*l)->next;
   struct list * templ=*l;
   *l=(*l)->next;
   free(templ);
   templ=NULL;
  }
}

void list_del(struct list ** l){
  while ((*l)->next!=(*l)){
    list_remove(l);
  }
  free(*l);
  *l=NULL;
}
/*
int list_test(void){

  struct list* list=list_create();

  assert(list!=NULL);
  printf("creation OK\n");
  int i=1;

  list_add(&list,(thread_t*)i);
  assert((int)list->t1==1);
  printf("ajout 1 OK\n");

  int j=2;
  list_add(&list,(thread_t*)j);
  assert((int)list->next->t1==1);
  assert((int)list->t1==2);
  assert((int)list->next->next->t1==2);
  printf("ajout 2 OK\n");

  int k=3;
  list_add(&list,(thread_t*)k);
  assert((int)list->next->t1==1);
  assert((int)list->t1==3);
  assert((int)list->next->next->t1==2);
  assert((int)list->next->next->next->t1==3);
  printf("ajout 3 OK\n");


  list_remove(&list);
  assert((int)list->next->t1==2);
  assert((int)list->t1==1);
  assert(list->next==list->previous);
  printf("remove OK\n");

  list_del(&list);
  assert(list==NULL);
  printf("delet OK\n");

return EXIT_SUCCESS;
}
*/
